package apresentacao;

import entidades.Cliente;
import java.awt.Frame;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import negocio.Central;
import negocio.Util;

public class TelaCadastroCliente extends JDialog {

    Central central;
    private boolean modifica;
    private Cliente clienteModificar;

    public TelaCadastroCliente(Frame pai, Central central, Cliente cliente) {
        super(pai,true);
        this.central = central;
        clienteModificar = cliente;
        this.modifica = (cliente!=null);
        initComponents();
        if (modifica) preencheComponentes(cliente);
        setLocationRelativeTo(null);
    }

    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        jTextField_nome = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextField_CPF = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextField_RG = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextField_endereco = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jTextField_telefone = new javax.swing.JTextField();
        jButton_cadastrar = new javax.swing.JButton();
        jButton_modificar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel1.setText("Cadastro de Cliente");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel2.setText("Nome:");

        jTextField_nome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField_nomeActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel3.setText("CPF:");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel4.setText("RG:");

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel5.setText("Endereço:");

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel9.setText("Telefone:");

        jButton_cadastrar.setText("Cadastrar");
        jButton_cadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_cadastrarActionPerformed(evt);
            }
        });

        jButton_modificar.setText("Modificar");
        jButton_modificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_modificarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextField_nome, javax.swing.GroupLayout.DEFAULT_SIZE, 334, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextField_CPF, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextField_RG, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextField_endereco, javax.swing.GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton_modificar, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 178, Short.MAX_VALUE)
                        .addComponent(jButton_cadastrar, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addGap(10, 10, 10)
                        .addComponent(jTextField_telefone, javax.swing.GroupLayout.DEFAULT_SIZE, 317, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField_nome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField_CPF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jTextField_RG, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextField_endereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField_telefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton_modificar)
                    .addComponent(jButton_cadastrar))
                .addContainerGap())
        );

        jButton_modificar.setVisible(false);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField_nomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField_nomeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField_nomeActionPerformed

    private void jButton_cadastrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_cadastrarActionPerformed
        Cliente c = new Cliente();
        c.setNome(jTextField_nome.getText());
        c.setCpf(jTextField_CPF.getText());
        c.setRg(jTextField_RG.getText());
        c.setTelefone(Integer.parseInt(jTextField_telefone.getText()));
        c.setEndereco(jTextField_endereco.getText());
        if(jTextField_nome.getText().equals("")) JOptionPane.showMessageDialog(null, "Por Favor, insira um nome!", "", JOptionPane.INFORMATION_MESSAGE);
        else if(jTextField_CPF.getText().equals("")) JOptionPane.showMessageDialog(null, "Por Favor, insira um CPF!", "", JOptionPane.INFORMATION_MESSAGE);
        else if(jTextField_RG.getText().equals("")) JOptionPane.showMessageDialog(null, "Por Favor, insira um RG!", "", JOptionPane.INFORMATION_MESSAGE);
        else if(jTextField_telefone.getText().equals("")) JOptionPane.showMessageDialog(null, "Por Favor, insira um numero de telefone!", "", JOptionPane.INFORMATION_MESSAGE);
        else if(jTextField_endereco.getText().equals("")) JOptionPane.showMessageDialog(null, "Por Favor, insira um endereço!", "", JOptionPane.INFORMATION_MESSAGE);
        else{
            try {
                central.insereCliente(c);
                JOptionPane.showMessageDialog(null, "Cliente cadastrado com sucesso!", "", JOptionPane.INFORMATION_MESSAGE);
                dispose();
            } catch (IOException ex) {
                Logger.getLogger(TelaCadastroCliente.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jButton_cadastrarActionPerformed

    private void jButton_modificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_modificarActionPerformed
        try {
            Cliente c = clienteModificar;
            c.setNome(jTextField_nome.getText());
            c.setCpf(jTextField_CPF.getText());
            c.setRg(jTextField_RG.getText());
            c.setTelefone(Integer.parseInt(jTextField_telefone.getText()));
            c.setEndereco(jTextField_endereco.getText());
            central.modificaCliente();
            Util.info("Cliente modificado com sucesso!");
            dispose();
        } catch (IOException ex) {
            Util.erro(ex.getMessage());
        }
    }//GEN-LAST:event_jButton_modificarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_cadastrar;
    private javax.swing.JButton jButton_modificar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTextField_CPF;
    private javax.swing.JTextField jTextField_RG;
    private javax.swing.JTextField jTextField_endereco;
    private javax.swing.JTextField jTextField_nome;
    private javax.swing.JTextField jTextField_telefone;
    // End of variables declaration//GEN-END:variables

    private void preencheComponentes(Cliente cliente) {
        jTextField_nome.setText(cliente.getNome());
        jTextField_CPF.setText(cliente.getCpf());
        jTextField_RG.setText(cliente.getRg());
        jTextField_endereco.setText(cliente.getEndereco());
        jTextField_telefone.setText(cliente.getTelefone()+"");
        jLabel1.setText("Atualizaçao de dados");
        jButton_cadastrar.setVisible(false);
        jButton_modificar.setVisible(true);
    }
}
